package net.sk.rm;

import java.util.HashMap;

import net.sk.pt.State;
import net.sk.web.Provider;

public class rmRobot implements Runnable {
	private final int loop_span = 3*1000;
	private boolean bStop = false;
	private Provider pr;
	final static String CN_GTI_DATA="cn_gti_data";
	private Object[] node_ids;
	
	public rmRobot(Provider pr,Object[] node_ids){
		this.pr = pr;
		this.node_ids = node_ids;
		randAll(State.ACTION_ADD);
	}
	private void randAll(char action){
		for(int i=0; i<node_ids.length; i++){
			pr.send(randValue((Integer)node_ids[i],action));
		}
	}
	private State randValue(int node_id,char action){
		State s = new State();
		s.setCn(CN_GTI_DATA);
		s.setAction(action);
		HashMap<String,Object> its = new HashMap<String,Object>();
		s.setItems(its);
		s.setKey(node_id);
		its.put("rssi", getscale(Math.random()*-100));
		its.put("tempr2", getscale(Math.random()*50));
		its.put("humi", getscale(Math.random()*20));
		return s;
	}
	private double getscale(double din){
		String s = String.format("%.2f",din);
		return Double.parseDouble(s);
	}
	public void stop(){
		bStop =  true;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(!bStop){
			try {
				Thread.sleep(loop_span);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			randAll(State.ACTION_UPDATE);
		}
		//通知删除状态
		randAll(State.ACTION_REMOVE);
	}

}
